// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Design/DLINKParser.h"

#include "gtest/gtest.h"

#include "DataStructure/Input/Design/DLINKData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Design/IDLINKService.h"

#include "Import/BDFImport/BDFParserBase.h"
using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure::FEM;
extern std::shared_ptr<DBServiceFactory> DB_SERVICEFACTORY;
extern std::vector<std::string>          dividedEntryInfo;

class DLINKParserTest : public ::testing::Test
{
  public:
    static void SetUpTestSuite ()
    {
        DB_SERVICEFACTORY->ClearDatabase ();
    }
};

TEST_F (DLINKParserTest, ParseEntry1)
{  //$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567
    std::vector<std::string> Entry = { "DLINK   1       3       0.0     1.0     1       1.00" };

    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseDLINK (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::DLINKData data;

    DB_SERVICEFACTORY->get<IDLINKService> ()->FindByIndex (0, data);

    ASSERT_TRUE (data.m_id == 1);
    ASSERT_TRUE (data.m_DVID == 3);
    ASSERT_TRUE (data.m_C0 == 0.0);
    ASSERT_TRUE (data.m_CMULT == 1.0);
    ASSERT_TRUE (data.m_IDVCList[0].m_C == 1);
    ASSERT_TRUE (data.m_IDVCList[0].m_INDV == 1.00);
}

TEST_F (DLINKParserTest, ParseEntry2)
{  //$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567
    std::vector<std::string> Entry = { "DLINK   10      2       0.1     0.33    4       2.0     6       -1.0    +000005F",
                                       "+000005F8       7.0" };

    DB_SERVICEFACTORY->ClearDatabase ();
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseDLINK (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::DLINKData data;

    DB_SERVICEFACTORY->get<IDLINKService> ()->FindByIndex (0, data);

    ASSERT_TRUE (data.m_id == 10);
    ASSERT_TRUE (data.m_DVID == 2);
    ASSERT_TRUE (data.m_C0 == 0.1);
    ASSERT_TRUE (data.m_CMULT == 0.33);
    ASSERT_TRUE (data.m_IDVCList[0].m_INDV == 4);
    ASSERT_TRUE (data.m_IDVCList[0].m_C == 2.0);
    ASSERT_TRUE (data.m_IDVCList[1].m_INDV == 6);
    ASSERT_TRUE (data.m_IDVCList[1].m_C == -1.0);
    ASSERT_TRUE (data.m_IDVCList[2].m_INDV == 8);
    ASSERT_TRUE (data.m_IDVCList[2].m_C == 7.0);
}

TEST_F (DLINKParserTest, ParseEntry3)
{
    //$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567
    std::vector<std::string> Entry = { "DLINK   102     2               1.      1       1." };

    // 清空数据库（确保测试环境干净）
    DB_SERVICEFACTORY->ClearDatabase ();

    // 分割词条并解析
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseDLINK (DB_SERVICEFACTORY, dividedEntryInfo);

    // 从数据库获取解析后的数据
    SG::DataStructure::FEM::DLINKData data;
    DB_SERVICEFACTORY->get<IDLINKService> ()->FindByIndex (0, data);

    // 断言验证解析结果
    ASSERT_TRUE (data.m_id == 102);
    ASSERT_TRUE (data.m_DVID == 2);
    ASSERT_TRUE (data.m_C0 == 0.0);
    ASSERT_TRUE (data.m_CMULT == 1.0);
    ASSERT_TRUE (data.m_IDVCList[0].m_C == 1);
    ASSERT_TRUE (data.m_IDVCList[0].m_INDV == 1.0);
}

TEST_F (DLINKParserTest, ParseEntry4)
{
    //$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567
    std::vector<std::string> Entry = { "DLINK   102     2                       1       1." };

    // 清空数据库（确保测试环境干净）
    DB_SERVICEFACTORY->ClearDatabase ();

    // 分割词条并解析
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseDLINK (DB_SERVICEFACTORY, dividedEntryInfo);

    // 从数据库获取解析后的数据
    SG::DataStructure::FEM::DLINKData data;
    DB_SERVICEFACTORY->get<IDLINKService> ()->FindByIndex (0, data);

    // 断言验证解析结果
    ASSERT_TRUE (data.m_id == 102);
    ASSERT_TRUE (data.m_DVID == 2);
    ASSERT_TRUE (data.m_C0 == 0.0);
    ASSERT_TRUE (data.m_CMULT == 1.0);
    ASSERT_TRUE (data.m_IDVCList[0].m_C == 1);
    ASSERT_TRUE (data.m_IDVCList[0].m_INDV == 1.0);
}